package com.mxb.utils;

import com.mxb.annotation.Autowired;
import com.mxb.annotation.Service;

import java.sql.SQLException;

/**
 * 事务管理器
 *
 * @Author: MaXiaobing
 * @Date: 2021/4/17 13:24
 */

@Service
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtils;
    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    // 开启事务
    public void beginTransaction() throws SQLException {
        System.out.println("开始事务：TransactionManager->beginTransaction()");
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }
    // 提交事务
    public void commit() throws SQLException {
        System.out.println("提交事务：TransactionManager->commit()");
        connectionUtils.getCurrentThreadConn().commit();
    }
    // 回滚事务
    public void rollback() throws SQLException {
        System.out.println("回滚事务：TransactionManager->rollback()");
        connectionUtils.getCurrentThreadConn().rollback();
    }

}
